package com.dykj.platform.iadapter.interceptor;

import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.aop.MethodBeforeAdvice;
import org.apache.struts2.ServletActionContext;
import com.dykj.platform.framework.common.paging.Hquery;

public class DataAuthBeforeAdvice implements MethodBeforeAdvice{
	private String dataAccessFieldNameMapping;
	public void before(Method method, Object[] arg1, Object arg2) throws Throwable {
		// TODO Auto-generated method stub
		if("createParaQuery".equals(method.getName())){
			Hquery hquery = (Hquery)arg1[0];
			String dataBindingTable = hquery.getDataAccessBindingTable();
			StringBuffer hql = new StringBuffer(hquery.getQueryString());
			boolean is = null!=dataBindingTable&&!"".equals(dataBindingTable);
			if(is){
				HttpServletRequest request = ServletActionContext.getRequest();
				String userid = null;
				Map map = (Map)request.getSession().getAttribute("userinfo");
				userid = map.get("username").toString();
				String[] table = dataBindingTable.split(",");
				if(table.length>0){
					for(int i=0;i<table.length;i++){
						hql.append(" and ");
						hql.append(table[i]);
						hql.append(".");
						hql.append(dataAccessFieldNameMapping);
						hql.append(" in(select distinct(g.org) from SysUser u,SysAct act,SysGroup g where u.authid = act.authid and act.groupid=g.org and u.userid = '");
						hql.append(userid);
						hql.append("')");
					}
				}
			}
			hquery.setQueryString(hql.toString());
		}
	}
	public String getDataAccessFieldNameMapping() {
		return dataAccessFieldNameMapping;
	}
	public void setDataAccessFieldNameMapping(String dataAccessFieldNameMapping) {
		this.dataAccessFieldNameMapping = dataAccessFieldNameMapping;
	}

}
